import config from '../url.config';
import Http from './gh-http';

import GlobalConfig from '../../config/index';
const Domain = GlobalConfig.Domain;
const ApiPath = GlobalConfig.ApiPath;
const FilePath = GlobalConfig.FilePath;
/**
 * method only support GEt || POST
 * options 
 *  {
 *    key:' url config key',
 *    data:{},
 *    headers:{},
 *    dataType : 'default json',
 * }
 * return Promise();
 * support Promise.prototype.finally
 */
class FetchService {
  constructor() { }
  static request(opts) {
    let { key } = opts;
    // 判断key 在urlconfig中正确配置
    if (!key || !config[key]) {
      throw new Error('key Unstated in url-config');
      return;
    }
    // 判断 mothod 暂时只支持 post get 其他方法禁用
    let mothod = config[key].method || 'GET';
    if (['GET', 'POST'].indexOf(mothod) == -1) {
      throw new Error('Method only support [Get,Post]');
      return false;
    }
    let options = {
      ...opts,
      url: Domain + ApiPath + config[key]['url'],
      method: mothod
    };
    return Http.request(options);
  }
}
// class FileClass {
//   constructor() { }
//   static uploadFile(opts) {
//     let options = {
//       ...opts,
//       url: Domain + ApiPath + opts.url,
//     }
//     return File.uploadFile(options);
//   }
//   static downloadFile(opts){
//     let options = {
//       ...opts,
//       url: Domain + FilePath + opts.url,
//     }
//     return File.downloadFile(options)
//   }
// }
const GHHttp = FetchService.request;

export {
  GHHttp
};